package com.chengwei.ch.busdemo.activity;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;
import com.chengwei.ch.busdemo.R;
import com.chengwei.ch.busdemo.base.BaseActivity;
import com.chengwei.ch.busdemo.stomp.Message;
import com.chengwei.ch.busdemo.utils.DensityUtil;
import com.chengwei.ch.busdemo.view.MqttManager;
import com.timmy.tdialog.TDialog;

import static com.chengwei.ch.busdemo.APP.app;
import static com.chengwei.ch.busdemo.APP.stomp;


@SuppressLint("Registered")
public class SignInActivity extends BaseActivity {
    private Button btnSignIn, btnDriverLogin, btnRegister;
    private TextView tvDesSignIn;

    private String queueName = "";
    private TDialog mTDialog = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_in);
        initView();
        initClickListener();
    }

    private void initView() {
        Float s = DensityUtil.getDensity(SignInActivity.this);
        Float dp = 1080 / (s / 160);
        Log.d("MainActivity", "density:" + s);
        Log.d("MainActivity", "dp:" + dp);
        // mScreensaver.exitApp();
        btnSignIn = findViewById(R.id.btnCheckSignIn);
        btnDriverLogin = findViewById(R.id.btnDriverLoginSignIn);
        btnRegister = findViewById(R.id.btnDriverLoginRegister);
        tvDesSignIn = findViewById(R.id.tvDesSignIn);
        new Handler().postDelayed(() -> {
            String tcsiId = "0fb98d6dcfd140e88e0769aadd4bec8f";
            queueName = "/" + tcsiId + ".waitPay.queue";
            String queueName1 = "/" + tcsiId + ".inSite.queue";
            //订阅队列
            //startQueue(queueName);
            //startQueue(queueName1);
            Log.d("BaseActivity:", "queueName:" + queueName);
        }, 1000);
    }

    private void initClickListener() {
        btnSignIn.setOnClickListener(this);
        btnDriverLogin.setOnClickListener(this);
        btnRegister.setOnClickListener(this);
    }

    /**
     * 订阅队列
     */
    private void startQueue(String queueName) {
        if (stomp != null) {
            try {
                stomp.queue(queueName, (Message msg) -> {
                    // 得到消息负载
                    String message = msg.getPayload();
                    Log.d("BaseActivity", "数据接收:" + message);
                    android.os.Message mMessage = new android.os.Message();
                    Bundle data = new Bundle();
                    //将获取到的String装载到msg中
                    data.putString("message", message);
                    mMessage.setData(data);
                    mMessage.what = 1;
                    //发消息到主线程
                    handler.sendMessage(mMessage);
                });
            } catch (Exception e) {
                ToastUtils.showShort("该队列已经被订阅过");
                Log.d("BaseActivity", "The destination [" + e + "] has already been subscribed!");
            }
        }
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull android.os.Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                Bundle data = msg.getData();
                String message = data.getString("message");
                tvDesSignIn.setText(message);
            }
        }
    };


    /**
     * 退订队列
     */
    private void unQueue(String queueName) {
        stomp.unqueue(queueName);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.btnCheckSignIn: {
                showDialog();
                new Handler().postDelayed(() -> {
                    if (mTDialog != null) {
                        mTDialog.dismiss();
                    }
                    intent.setClass(SignInActivity.this, MainActivity.class);
                    startActivity(intent);
                }, 200);
                break;
            }
            case R.id.btnDriverLoginSignIn: {
                intent.setClass(SignInActivity.this, LoginActivity.class);
                startActivity(intent);
                break;
            }
            case R.id.btnDriverLoginRegister: {
                intent.setClass(SignInActivity.this, RegisterActivity.class);
                startActivity(intent);
                break;
            }
            default:
                break;
        }
    }

    private void showDialog() {
        mTDialog = new TDialog.Builder(getSupportFragmentManager())
                .setLayoutRes(R.layout.dialog_sign_in)    //设置弹窗展示的xml布局
                .setDimAmount(0.6f)
                .setScreenWidthAspect(SignInActivity.this, 0.85f)   //设置弹窗宽度(参数aspect为屏幕宽度比例 0 - 1f)
                .setScreenHeightAspect(SignInActivity.this, 0f)  //设置弹窗高度(参数aspect为屏幕宽度比例 0 - 1f)
                .setCancelableOutside(false)
                .create()   //创建TDialog
                .show();    //展示
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //mScreensaver.resetTime();
        app.exitApp();
        //退订队列
        unQueue(queueName);
    }
}
